﻿using Furion.Schedule;
using Microsoft.Extensions.DependencyInjection;

namespace GnpFine.Application.NmpaSystem;

/// <summary>
/// 清理日志作业任务
/// </summary>
[JobDetail("InstrumentPurchJob", Description = "器械采购", GroupName = "default", Concurrent = false)]
[PeriodHours(8, TriggerId = "trigger_InstrumentPurchJob", Description = "器械采购", MaxNumberOfRuns = 5,  RunOnStart = true, StartNow = true)]
public class InstrumentPurchJob : IJob
{
    private readonly ISqlSugarClient _db;
    public InstrumentPurchJob(ISqlSugarClient db)
    {
        _db = db;
    }

    public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {

        Log.Information("\n-----------器械采购更新--任务开始----------");

        // 获取定时任务的参数
        var requestParam = context.JobDetail.GetProperty("sqlwhere").ToString().Trim();
        
        // 从金蝶服务器获取数据
        ResponseModel<InstrumentStock> kDCusinvdata = KdCloudOpertionUtils.GetCusdata<InstrumentStock>("GnFine.Yaojian.WebAPI.ServicesStub.GetCustInstrumentPurch.ExecuteService,GnFine.Yaojian.WebAPI.ServicesStub", requestParam);
        
        // 更新数据库
        var _childDb = _db.AsTenant().GetConnectionScopeWithAttr<InstrumentStock>();
        var a = _childDb.Ado.ExecuteCommand("delete  from [InstrumentStock] ");
        var b = _childDb.AsTenant().InsertableWithAttr(kDCusinvdata.data).ExecuteCommand();
   
        Log.Information($"器械采购更新--任务结束:数据获取{kDCusinvdata.rowcount}条,数据插入{b}条，删除{a}条\n");

    }
}
